package modelo.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.*;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Index;

@Entity
@Table(name = "trajeto")
@Inheritance(strategy = InheritanceType.JOINED)
public class Trajeto implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@Index(name = "id_trajeto_pk")
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id_trajeto")
	private Long id_trajeto;

	@Column(name = "start")
	private String start;

	@Column(name = "end")
	private String end;

	@Transient
	private int tempo;

	@Column(name = "distancia")
	private int distancia;

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "id_usuario", referencedColumnName = "id_usuario")
	private Usuario id_usuario;

	@OneToMany(mappedBy = "id_trajeto", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
	@Fetch(FetchMode.SUBSELECT)
	private Collection<LatLng> cordenadas;

	@Transient
	// array de pontos do passos do trajeto
	private Collection<LatLng> passos = new ArrayList<LatLng>();
	@Transient
	// array de string dos passos do trajeto
	private Collection<String> routes = new ArrayList<String>();

	public Trajeto() {

	}


	/**
	 * @return the id_trajeto
	 */
	public Long getId_trajeto() {
		return id_trajeto;
	}

	/**
	 * @param id_trajeto
	 *            the id_trajeto to set
	 */
	public void setId_trajeto(Long id_trajeto) {
		this.id_trajeto = id_trajeto;
	}

	public Collection<String> getRoutes() {
		return routes;
	}

	public void setRoutes(Collection<String> routes) {
		this.routes = routes;
	}

	
	 public Trajeto(String result) {
		 setPontosByRoute(result);
	 }

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}

	public int getTempo() {
		return tempo;
	}

	public void setTempo(int tempo) {
		this.tempo = tempo;
	}

	public int getDistancia() {
		return distancia;
	}

	public void setDistancia(int distancia) {
		this.distancia = distancia;
	}

	public Collection<LatLng> getPontos() {
		return cordenadas;
	}

	public void setPontos(Collection<LatLng> pontos) {
		this.cordenadas = pontos;
	}

	public Collection<LatLng> getPassos() {
		return passos;
	}

	public void setPassos(Collection<LatLng> passos) {
		this.passos = passos;
	}

	public void setPontosByRoute(String result) {
		String[] temp = result.split(",");
		for (int i = 0; i < temp.length; i += 2) {
			System.out.println("Lat["+ temp[i] +"] Lng[" +temp[i+1] + "]");
			passos.add(new LatLng(Double.parseDouble(temp[i]), Double
					.parseDouble(temp[i + 1])));
		}
	}

	/**
	 * @return the id_usuario
	 */
	public Usuario getId_usuario() {
		return id_usuario;
	}

	/**
	 * @param id_usuario
	 *            the id_usuario to set
	 */
	public void setId_usuario(Usuario id_usuario) {
		this.id_usuario = id_usuario;
	}

}
